
#include <ros/ros.h>
#include <std_msgs/String.h>

//这个回调函数的参数为ConstPtr智能指针
void chatter_callback(const std_msgs::String::ConstPtr &msg)
{
    ROS_INFO("I heard: %s", msg->data.c_str());
}

//这个回调函数的参数为Const引用
void chatter_callback1(const std_msgs::String& msg)
{
    ROS_INFO("I heard: %s", msg.data.c_str());
}

int main(int argc, char *argv[])
{
    setlocale(LC_ALL, "");
    ros::init(argc, argv, "listener");

    ros::NodeHandle nh;

    // ros::Subscriber chatter_sub = nh.subscribe<std_msgs::String>("chatter", 100, chatter_callback);
    ros::Subscriber chatter_sub = nh.subscribe("chatter", 100, chatter_callback1);
    //订阅消息回调处理函数，spin函数会陷入死循环，没有消息时会阻塞也不占用CPU资源
    ros::spin();
    return 0;
}